//
//  KAPieChartView.m
//  KeepAccounts
//
//  Created by honeyrr on 2017/10/26.
//  Copyright © 2017年 honeyrr. All rights reserved.
//

#import "KAPieChartView.h"

@interface KAPieChartView()<PieChartDelegate>

@property(nonatomic, strong) XYPieChartView *pieChartView;

@end

@implementation KAPieChartView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setUpUI];
    }
    return self;
}

-(void)setPieMarr:(NSArray *)pieMarr
{
    _pieMarr = pieMarr;
    if ([pieMarr[0][0] count] != 0) {
        self.pieChartView.pieChartData = pieMarr[0];
    } else {
        self.pieChartView.pieChartData = KANoData;
    }
    
}

-(void)setAllIncomeMoney:(NSInteger)allIncomeMoney
{
    _allIncomeMoney = allIncomeMoney;
    [self.pieChartView setAmountText:[NSString stringWithFormat:@"总收入:￥%ld",allIncomeMoney]];
    [self.pieChartView reloadChart];
}

-(void) setUpUI
{
    CGFloat Width = self.frame.size.width;
    CGRect pieRect = CGRectMake((Width - Width * 0.6) / 2, 8, Width * 0.6, Width * 0.6);
    // 初始化饼图
    self.pieChartView = [[XYPieChartView alloc]initWithFrame:pieRect];
    self.pieChartView.delegate = self;
    //当有一项数据的百分比小于你所校验的数值时,会将该项数值百分比移出饼图展示(校验数值从0~100)
    [self.pieChartView setCheckLessThanPercent:3];
    // 设置圆心标题 （NSString类型）
    // 刷新加载
    [self.pieChartView reloadChart];
    [self addSubview:self.pieChartView];
}

#pragma mark - 圆心点击事件
-(void)onCenterClick:(XYPieChartView *)PieChartView
{
    if (!self.isOutlay) {
        [self.pieChartView setAmountText:[NSString stringWithFormat:@"总支出:￥%ld",self.allOutlayMoney]];
        
        if ([self.pieMarr[1][0] count] != 0) {
            self.pieChartView.pieChartData = self.pieMarr[1];
        } else {
            self.pieChartView.pieChartData = KANoData;
        }
    } else {
        [self.pieChartView setAmountText:[NSString stringWithFormat:@"总收入:￥%ld",self.allIncomeMoney]];
        
        if ([self.pieMarr[0][0] count] != 0) {
            self.pieChartView.pieChartData = self.pieMarr[0];
        } else {
            self.pieChartView.pieChartData = KANoData;
        }
    }
    self.isOutlay = !self.isOutlay;
    if ([self.delegate respondsToSelector:@selector(onCenterClick:)]) {
        [self.delegate onCenterClick:self];
    }
}

-(void)reloadPieChartView
{
    [self.pieChartView reloadChart];
}

- (void)selectedFinish:(XYPieChartView *)pieChartView index:(NSInteger)index selectedType:(NSDictionary *)selectedType
{
    
}




@end
